package com.hthy.futures.ui.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;

import com.hthy.futures.R;
import com.hthy.futures.api.ST_TradeApi;
import com.hthy.futures.base.BaseListFragment;
import com.hthy.futures.base.CommonAdapter;
import com.hthy.futures.base.CommonViewHolder;
import com.hthy.futures.bean.NoticeData;
import com.hthy.futures.ui.activity.NoticeContentActivity;
import com.hthy.futures.utils.DataUtils;
import com.hthy.futures.utils.ErrorUtils;
import com.hthy.futures.utils.LogUtils;
import com.vilyever.socketclient.SocketClient;

import net.hthy.bean.Trade;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import cn.bingoogolapple.refreshlayout.BGARefreshLayout;

/**
 * 公告
 * Author:  Mr.bingley
 * Version:
 * Date:  2017/3/6
 */

public class NoticeFragment extends BaseListFragment {

    public static String noticeId;
    private CommonAdapter<NoticeData> mNoticeAdapter;


    @Override
    protected void initWidget(View root) {
        super.initWidget(root);

        getNoticeData();

        mNoticeAdapter = new CommonAdapter<NoticeData>(getActivity(), R.layout.adapter_notice) {
            @Override
            public void convert(CommonViewHolder vh, NoticeData item) {
                vh.setText(R.id.tv_noticeer, "发布者 " + item.get_$8111() + "(" + item.get_$8110() + ")");
                // 需要转换下
                vh.setText(R.id.tv_time, "时间 " + DataUtils.stampToDate(item.get_$8113()));
                //
                vh.setText(R.id.tv_notice, "" + item.get_$8112());
            }
        };

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                NoticeData item = mNoticeAdapter.getItem(position);
                noticeId = item.get_$8110();
                startActivity(new Intent(getActivity(), NoticeContentActivity.class));
            }
        });
        listView.setAdapter(mNoticeAdapter);
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout bgaRefreshLayout) {
        super.onBGARefreshLayoutBeginRefreshing(bgaRefreshLayout);
        getNoticeData();
    }

    /**
     * 获取公告列表
     */
    private void getNoticeData() {
/*        String noticeStr = ST_TradeApi.searchGoods("2051");
        LogUtils.d(noticeStr);

        socketClient.sendString(noticeStr);*/
        socketClient.sendData(ST_TradeApi.searchGoods("2051"));
    }

    @Override
    public void onResume() {
        super.onResume();
        getNoticeData();
    }

    @Override
    protected void onResponse(SocketClient client, String responMes, byte[] subBytes) {
        super.onResponse(client, responMes, subBytes);
        LogUtils.e(responMes);
        try {
            Trade.QueryNoticeListResponseMsg queryNoticeListResponseMsg = Trade.QueryNoticeListResponseMsg.parseFrom(subBytes);
            int iErrorCode = queryNoticeListResponseMsg.getResponseMsg().getIErrorCode();
            if (0 == iErrorCode) {
                endResponse();
                List<NoticeData> datas = new ArrayList<>();
                List<Trade.NoticeMeta> noticeMetaList = queryNoticeListResponseMsg.getNoticeMetaList();
                for (Trade.NoticeMeta noticeMeta : noticeMetaList) {
                    NoticeData noticeData = new NoticeData();
                    noticeData.set_$8110(noticeMeta.getUiNoticeUid()+"");
                    noticeData.set_$8111(noticeMeta.getSsNoticeAuthor());
                    noticeData.set_$8112(noticeMeta.getSsNoticeTitle());
                    noticeData.set_$8113(noticeMeta.getIiNoticePublishTime()+"");
                    datas.add(noticeData);
                }
                if (mNoticeAdapter.getDatas().size() > 0) {
                    mNoticeAdapter.clear();
                }
                Collections.reverse(datas);
                mNoticeAdapter.addItem(datas);
            } else {
                ErrorUtils.sendErrorMes(getActivity(), iErrorCode + "");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
